package com.hbisedm.interninfosys.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;
import com.hbisedm.interninfosys.config.OssConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class AliyunOssUtil {
    @Autowired
    private OssConfiguration config;

    public String upload(File file) {
        if (file == null) {
            return null;
        }

        OSSClient client = getClient();

        String bucketName = config.getBucketName();
        String fileHost = config.getFileHost();

        //定义子文件的格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = format.format(new Date());


        try {
            //判断桶是否存在
            if (!client.doesBucketExist(bucketName)) {
                //创建桶
                client.createBucket(bucketName);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                //设置访问权限为公共读
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                //发起创建桶的请求
                client.createBucket(createBucketRequest);
            }

            String test = file.getName();
            int index = test.lastIndexOf('.');
            String suffix = test.substring(index);


            test = new String(test.getBytes("UTF-8"),"UTF-8");

            //当桶存在时,进行文件上传
            //设置文件路径和名称
            String fileName = fileHost + "/" + (dateStr + "/" + UUID.randomUUID().toString().replace("-", "") + "-" + test);
            PutObjectResult result = null;
            //传图片
            if(".jpg".equals(suffix)) {
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentType("image/jpg");
                result = client.putObject(bucketName, fileName, file, objectMetadata);
            } else {
                result = client.putObject(new PutObjectRequest(bucketName, fileName, file));
            }
            client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);

            //文件上传成功后,返回当前文件的路径
            if (result != null) {
                return fileName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                client.shutdown();
            }
        }

        return null;
    }

    private OSSClient getClient() {
        String endPoint = config.getEndPoint();
        String keyId = config.getAccessKeyId();
        String keySecret = config.getAccessKeySecret();
        //阿里云文件上传客户端
        OSSClient client = new OSSClient(endPoint, keyId, keySecret);
        return client;
    }

    public void getFile(String url, String localFile) {
        String endPoint = config.getEndPoint();
        String keyId = config.getAccessKeyId();
        String keySecret = config.getAccessKeySecret();
        String bucketName = config.getBucketName();
        String fileHost = config.getFileHost();

        //定义子文件的格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = format.format(new Date());

        //阿里云文件上传客户端
        OSSClient client = new OSSClient(endPoint, keyId, keySecret);

        String objName = url;
        File file = new File(localFile);
        client.getObject(new GetObjectRequest(bucketName, objName), file);


    }

    public void deleteFile(String key) {
        OSSClient client = getClient();
        String bucketName = config.getBucketName();
        client.deleteObject(bucketName, key);
    }
}